#include "futon.h"
#include "ui_futon.h"


using namespace std;

Futon::Futon(QWidget *parent): QMainWindow(parent), ui(new Ui::Futon)
{
    ui->setupUi(this);
    add_bookmarks();

    history.position = -1;

    ui->dicttext->setFocus();
    ui->back->setEnabled(false);
    ui->next->setEnabled(false);

    QMenu *m = new QMenu();

    m->addAction("Clear history", this, SLOT(clear_history()));
    m->addAction("Clear bookmarks", this, SLOT(clear_bookmarks()));
    m->addSeparator();
    QAction *s = m->addAction("View sidebar", this, SLOT(sider(bool)));
    s->setCheckable(true);
    s->setChecked(true);
    m->addSeparator();
    m->addAction("Report a bug", this, SLOT(report_bug()));
    m->addSeparator();
    m->addAction("About", this, SLOT(about()));
    ui->menu->setMenu(m);
}

Futon::~Futon()
{
    setAttribute(Qt::WA_DeleteOnClose, true);
    delete ui;
}

void Futon::add_bookmarks()
{
    change_icon();
    ui->bookmarks_list->clear();
    for (int i = 0; i < bookmarks.Size(); i++)
        ui->bookmarks_list->addItem(new QListWidgetItem(bookmarks.Get(i)));
}

void Futon::on_search_textEdited()
{
    QFont newFont("Arial", 9, QFont::StyleItalic, false);
    ui->search->setFont(newFont);
}

void Futon::on_search_returnPressed()
{
    actual = ui->search->text();
    change_icon();
    if (history.Get(history.position) != actual)
    {
        history.Remove(history.Begin()+history.position+1, history.End());
        history.Add(ui->search->text());
        history.position++;
    }

    QString html = dbase.Ask();
    ui->dicttext->setHtml(html);

    if (history.position >= 1)
    {
        ui->back->setEnabled(true);
        ui->next->setFocus();
    }
    if (history.position >= history.Size()-1)
    {
        ui->next->setEnabled(false);
        ui->back->setFocus();
    }
    ui->bookmark_button->setEnabled(true);
    ui->dicttext->setFocus();
}

void Futon::on_back_clicked()
{
    history.position--;
    ui->search->setText(history.Get(history.position));
    actual = history.Get(history.position);
    change_icon();
    ui->next->setEnabled(true);
    if (history.position <= 0)
    {
        ui->back->setEnabled(false);
        ui->next->setFocus();
    }
}

void Futon::on_next_clicked()
{
    history.position++;
    ui->search->setText(history.Get(history.position));
    actual = history.Get(history.position);
    change_icon();
    ui->back->setEnabled(true);
    if (history.position >= history.Size()-1)
    {
        ui->next->setEnabled(false);
        ui->back->setFocus();
    }
}

void Futon::clear_history()
{
    history.RemoveAll();
    history.position = -1;
    ui->back->setEnabled(false);
    ui->next->setEnabled(false);
}

void Futon::clear_bookmarks()
{
    if (bookmarks.RemoveAll())
        ui->bookmarks_list->clear();
}

void Futon::about()
{
    About a;
    a.exec();
}

void Futon::sider(bool c)
{
    ui->sider->setVisible(c);
}

void Futon::report_bug()
{
    QDesktopServices open;
    QUrl url("https://bugs.launchpad.net/futon/+filebug");
    open.openUrl(url);
}

void Futon::change_icon()
{
    QIcon icon;
    if (bookmarks.HasEntry(actual))
    {
        icon.addFile(QString::fromUtf8(":/Ikony/Ikony/minus_48.png"), QSize(), QIcon::Normal, QIcon::On);
        ui->bookmark_button->setIcon(icon);
        ui->bookmark_button->setIconSize(QSize(32, 32));
    }
    else
    {
        icon.addFile(QString::fromUtf8(":/Ikony/Ikony/knewstuff.png"), QSize(), QIcon::Normal, QIcon::On);
        ui->bookmark_button->setIcon(icon);
        ui->bookmark_button->setIconSize(QSize(32, 32));
    }
}

void Futon::on_bookmark_button_clicked()
{
    if (!bookmarks.HasEntry(actual))
        bookmarks.Add(actual);
    else
        bookmarks.Remove(actual);
    this->add_bookmarks();
}

void Futon::on_search_editingFinished()
{
    QString entry = ui->search->text();
    entry[0] = entry[0].toUpper();
    ui->search->setText(entry);
}

void Futon::on_bookmarks_list_itemClicked(QListWidgetItem *item)
{
    ui->search->setText(ui->bookmarks_list->item(ui->bookmarks_list->currentRow())->text());
    on_search_returnPressed();
}

void Futon::on_bookmarks_list_customContextMenuRequested(const QPoint &pos)
{
    QMenu contextMenu(tr("Context menu"), this);
    contextMenu.addAction("Remove", this, SLOT(con_remove()));
    int x = pos.x();
    int y = pos.y();
    QPoint p(x, 50+y);
    contextMenu.exec(mapToGlobal(p));
}

void Futon::con_remove()
{
    bookmarks.Remove(ui->bookmarks_list->item(ui->bookmarks_list->currentRow())->text());
    this->add_bookmarks();
}
